package c.c.a;

import android.net.Uri;
import android.net.Uri.Builder;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Pair;
import com.bytedance.embedapplog.AppLog;
import com.bytedance.embedapplog.util.UriConfig;
import com.huawei.hms.framework.common.ContainerUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import org.json.JSONObject;

public class q {
    public static final String[] a = new String[]{"GET", "POST"};
    public static JSONObject b;
    public static final String[] c;
    public static final String[] d = new String[]{"aid", "version_code", "ab_client", "ab_version", "ab_feature", "ab_group", "iid", "device_platform"};
    public static final String[] e;
    public static UriConfig f = UriConfig.creatUriConfig(0);

    static {
        String str = "tt_data";
        c = new String[]{"aid", "app_version", str};
        e = new String[]{str, "device_platform"};
    }

    /* JADX WARNING: Removed duplicated region for block: B:88:0x01c4 A:{SYNTHETIC, Splitter:B:88:0x01c4} */
    /* JADX WARNING: Removed duplicated region for block: B:88:0x01c4 A:{SYNTHETIC, Splitter:B:88:0x01c4} */
    /* JADX WARNING: Removed duplicated region for block: B:88:0x01c4 A:{SYNTHETIC, Splitter:B:88:0x01c4} */
    /* JADX WARNING: Missing block: B:74:0x01a9, code skipped:
            if (r7 == null) goto L_0x01cc;
     */
    /* JADX WARNING: Missing block: B:76:?, code skipped:
            r7.close();
     */
    /* JADX WARNING: Missing block: B:91:0x01c9, code skipped:
            if (r7 != null) goto L_0x01ab;
     */
    public static java.lang.String a(int r6, java.lang.String r7, java.util.HashMap<java.lang.String, java.lang.String> r8, byte[] r9) {
        /*
        r0 = "gzip";
        r1 = c.c.a.m0.b;
        r2 = 0;
        if (r1 == 0) goto L_0x0081;
    L_0x0007:
        r1 = new java.lang.StringBuilder;
        r1.<init>();
        r3 = "http: ";
        r1.append(r3);
        r1.append(r7);
        r1 = r1.toString();
        c.c.a.m0.a(r1, r2);
        if (r8 == 0) goto L_0x006e;
    L_0x001d:
        r1 = r8.entrySet();
        r1 = r1.iterator();
    L_0x0025:
        r3 = r1.hasNext();
        if (r3 == 0) goto L_0x006e;
    L_0x002b:
        r3 = r1.next();
        r3 = (java.util.Map.Entry) r3;
        r4 = r3.getKey();
        r4 = (java.lang.CharSequence) r4;
        r4 = android.text.TextUtils.isEmpty(r4);
        if (r4 != 0) goto L_0x0025;
    L_0x003d:
        r4 = r3.getValue();
        r4 = (java.lang.CharSequence) r4;
        r4 = android.text.TextUtils.isEmpty(r4);
        if (r4 != 0) goto L_0x0025;
    L_0x0049:
        r4 = "http headers key:";
        r4 = c.b.a.a.a.g(r4);
        r5 = r3.getKey();
        r5 = (java.lang.String) r5;
        r4.append(r5);
        r5 = " value:";
        r4.append(r5);
        r3 = r3.getValue();
        r3 = (java.lang.String) r3;
        r4.append(r3);
        r3 = r4.toString();
        c.c.a.m0.a(r3, r2);
        goto L_0x0025;
    L_0x006e:
        if (r9 == 0) goto L_0x0081;
    L_0x0070:
        r1 = "http data length:";
        r1 = c.b.a.a.a.g(r1);
        r3 = r9.length;
        r1.append(r3);
        r1 = r1.toString();
        c.c.a.m0.a(r1, r2);
    L_0x0081:
        r1 = new java.net.URL;	 Catch:{ all -> 0x01ba }
        r1.<init>(r7);	 Catch:{ all -> 0x01ba }
        r7 = r1.openConnection();	 Catch:{ all -> 0x01ba }
        r7 = (java.net.HttpURLConnection) r7;	 Catch:{ all -> 0x01ba }
        if (r6 != 0) goto L_0x0093;
    L_0x008e:
        r1 = 0;
        r7.setDoOutput(r1);	 Catch:{ all -> 0x01ba }
        goto L_0x009d;
    L_0x0093:
        r1 = 1;
        if (r6 != r1) goto L_0x009a;
    L_0x0096:
        r7.setDoOutput(r1);	 Catch:{ all -> 0x01ba }
        goto L_0x009d;
    L_0x009a:
        c.c.a.m0.b(r2);	 Catch:{ all -> 0x01ba }
    L_0x009d:
        r1 = a;	 Catch:{ all -> 0x01ba }
        r6 = r1[r6];	 Catch:{ all -> 0x01ba }
        r7.setRequestMethod(r6);	 Catch:{ all -> 0x01ba }
        if (r8 == 0) goto L_0x00ec;
    L_0x00a6:
        r6 = r8.isEmpty();	 Catch:{ all -> 0x01ba }
        if (r6 != 0) goto L_0x00ec;
    L_0x00ac:
        r6 = r8.entrySet();	 Catch:{ all -> 0x01ba }
        r6 = r6.iterator();	 Catch:{ all -> 0x01ba }
    L_0x00b4:
        r8 = r6.hasNext();	 Catch:{ all -> 0x01ba }
        if (r8 == 0) goto L_0x00ec;
    L_0x00ba:
        r8 = r6.next();	 Catch:{ all -> 0x01ba }
        r8 = (java.util.Map.Entry) r8;	 Catch:{ all -> 0x01ba }
        r1 = r8.getKey();	 Catch:{ all -> 0x01ba }
        r1 = (java.lang.CharSequence) r1;	 Catch:{ all -> 0x01ba }
        r1 = android.text.TextUtils.isEmpty(r1);	 Catch:{ all -> 0x01ba }
        if (r1 != 0) goto L_0x00e8;
    L_0x00cc:
        r1 = r8.getValue();	 Catch:{ all -> 0x01ba }
        r1 = (java.lang.CharSequence) r1;	 Catch:{ all -> 0x01ba }
        r1 = android.text.TextUtils.isEmpty(r1);	 Catch:{ all -> 0x01ba }
        if (r1 != 0) goto L_0x00e8;
    L_0x00d8:
        r1 = r8.getKey();	 Catch:{ all -> 0x01ba }
        r1 = (java.lang.String) r1;	 Catch:{ all -> 0x01ba }
        r8 = r8.getValue();	 Catch:{ all -> 0x01ba }
        r8 = (java.lang.String) r8;	 Catch:{ all -> 0x01ba }
        r7.addRequestProperty(r1, r8);	 Catch:{ all -> 0x01ba }
        goto L_0x00b4;
    L_0x00e8:
        c.c.a.m0.b(r2);	 Catch:{ all -> 0x01ba }
        goto L_0x00b4;
    L_0x00ec:
        r6 = "Accept-Encoding";
        r7.setRequestProperty(r6, r0);	 Catch:{ all -> 0x01ba }
        if (r9 == 0) goto L_0x0109;
    L_0x00f3:
        r6 = r9.length;	 Catch:{ all -> 0x01ba }
        if (r6 <= 0) goto L_0x0109;
    L_0x00f6:
        r6 = new java.io.DataOutputStream;	 Catch:{ all -> 0x01ba }
        r8 = r7.getOutputStream();	 Catch:{ all -> 0x01ba }
        r6.<init>(r8);	 Catch:{ all -> 0x01ba }
        r6.write(r9);	 Catch:{ all -> 0x01b6 }
        r6.flush();	 Catch:{ all -> 0x01b6 }
        r6.close();	 Catch:{ all -> 0x01b6 }
        goto L_0x010a;
    L_0x0109:
        r6 = r2;
    L_0x010a:
        r8 = r7.getResponseCode();	 Catch:{ all -> 0x01b6 }
        r9 = "http_code";
        r1 = 200; // 0xc8 float:2.8E-43 double:9.9E-322;
        if (r8 != r1) goto L_0x017a;
    L_0x0114:
        r8 = r7.getContentLength();	 Catch:{ all -> 0x01b6 }
        r3 = 10240; // 0x2800 float:1.4349E-41 double:5.059E-320;
        if (r8 >= r3) goto L_0x0174;
    L_0x011c:
        r8 = r7.getInputStream();	 Catch:{ all -> 0x01b6 }
        r7 = r7.getContentEncoding();	 Catch:{ all -> 0x01b6 }
        r7 = r0.equalsIgnoreCase(r7);	 Catch:{ all -> 0x01b6 }
        if (r7 == 0) goto L_0x013a;
    L_0x012a:
        r7 = new java.io.BufferedReader;	 Catch:{ all -> 0x01b6 }
        r0 = new java.io.InputStreamReader;	 Catch:{ all -> 0x01b6 }
        r3 = new java.util.zip.GZIPInputStream;	 Catch:{ all -> 0x01b6 }
        r3.<init>(r8);	 Catch:{ all -> 0x01b6 }
        r0.<init>(r3);	 Catch:{ all -> 0x01b6 }
        r7.<init>(r0);	 Catch:{ all -> 0x01b6 }
        goto L_0x0144;
    L_0x013a:
        r7 = new java.io.BufferedReader;	 Catch:{ all -> 0x01b6 }
        r0 = new java.io.InputStreamReader;	 Catch:{ all -> 0x01b6 }
        r0.<init>(r8);	 Catch:{ all -> 0x01b6 }
        r7.<init>(r0);	 Catch:{ all -> 0x01b6 }
    L_0x0144:
        r0 = new java.lang.StringBuilder;	 Catch:{ all -> 0x0170 }
        r8 = r8.available();	 Catch:{ all -> 0x0170 }
        r0.<init>(r8);	 Catch:{ all -> 0x0170 }
    L_0x014d:
        r8 = r7.readLine();	 Catch:{ all -> 0x0170 }
        if (r8 == 0) goto L_0x015c;
    L_0x0153:
        r0.append(r8);	 Catch:{ all -> 0x0170 }
        r8 = "\n";
        r0.append(r8);	 Catch:{ all -> 0x0170 }
        goto L_0x014d;
    L_0x015c:
        r8 = r0.toString();	 Catch:{ all -> 0x0170 }
        r0 = new org.json.JSONObject;	 Catch:{ all -> 0x016d }
        r0.<init>(r8);	 Catch:{ all -> 0x016d }
        r0.put(r9, r1);	 Catch:{ all -> 0x016d }
        r8 = r0.toString();	 Catch:{ all -> 0x016d }
        goto L_0x01a2;
    L_0x016d:
        r9 = move-exception;
        goto L_0x01bf;
    L_0x0170:
        r8 = move-exception;
        r9 = r8;
        r8 = r2;
        goto L_0x01bf;
    L_0x0174:
        c.c.a.m0.b(r2);	 Catch:{ all -> 0x01b6 }
        r7 = r2;
        r8 = r7;
        goto L_0x01a2;
    L_0x017a:
        r7 = new org.json.JSONObject;	 Catch:{ all -> 0x01b6 }
        r7.<init>();	 Catch:{ all -> 0x01b6 }
        r7 = r7.put(r9, r8);	 Catch:{ all -> 0x01b6 }
        r7 = r7.toString();	 Catch:{ all -> 0x01b6 }
        r9 = new java.lang.RuntimeException;	 Catch:{ all -> 0x01b1 }
        r0 = new java.lang.StringBuilder;	 Catch:{ all -> 0x01b1 }
        r0.<init>();	 Catch:{ all -> 0x01b1 }
        r1 = "HttpCode:";
        r0.append(r1);	 Catch:{ all -> 0x01b1 }
        r0.append(r8);	 Catch:{ all -> 0x01b1 }
        r8 = r0.toString();	 Catch:{ all -> 0x01b1 }
        r9.<init>(r8);	 Catch:{ all -> 0x01b1 }
        r9.printStackTrace();	 Catch:{ all -> 0x01b1 }
        r8 = r7;
        r7 = r2;
    L_0x01a2:
        if (r6 == 0) goto L_0x01a9;
    L_0x01a4:
        r6.close();	 Catch:{ IOException -> 0x01a8 }
        goto L_0x01a9;
    L_0x01a9:
        if (r7 == 0) goto L_0x01cc;
    L_0x01ab:
        r7.close();	 Catch:{ IOException -> 0x01af }
        goto L_0x01cc;
        goto L_0x01cc;
    L_0x01b1:
        r8 = move-exception;
        r9 = r8;
        r8 = r7;
        r7 = r2;
        goto L_0x01bf;
    L_0x01b6:
        r7 = move-exception;
        r9 = r7;
        r7 = r2;
        goto L_0x01be;
    L_0x01ba:
        r6 = move-exception;
        r9 = r6;
        r6 = r2;
        r7 = r6;
    L_0x01be:
        r8 = r7;
    L_0x01bf:
        c.c.a.m0.b(r9);	 Catch:{ all -> 0x01e5 }
        if (r6 == 0) goto L_0x01c9;
    L_0x01c4:
        r6.close();	 Catch:{ IOException -> 0x01c8 }
        goto L_0x01c9;
    L_0x01c9:
        if (r7 == 0) goto L_0x01cc;
    L_0x01cb:
        goto L_0x01ab;
    L_0x01cc:
        r6 = c.c.a.m0.b;
        if (r6 == 0) goto L_0x01e4;
    L_0x01d0:
        r6 = new java.lang.StringBuilder;
        r6.<init>();
        r7 = "http response: ";
        r6.append(r7);
        r6.append(r8);
        r6 = r6.toString();
        c.c.a.m0.a(r6, r2);
    L_0x01e4:
        return r8;
    L_0x01e5:
        r8 = move-exception;
        if (r6 == 0) goto L_0x01ed;
    L_0x01e8:
        r6.close();	 Catch:{ IOException -> 0x01ec }
        goto L_0x01ed;
    L_0x01ed:
        if (r7 == 0) goto L_0x01f2;
    L_0x01ef:
        r7.close();	 Catch:{ IOException -> 0x01f2 }
    L_0x01f2:
        throw r8;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.c.a.q.a(int, java.lang.String, java.util.HashMap, byte[]):java.lang.String");
    }

    public static String b(String str) {
        if (TextUtils.isEmpty(str) || !AppLog.getEncryptAndCompress()) {
            return str;
        }
        Uri parse = Uri.parse(str);
        String query = parse.getQuery();
        ArrayList arrayList = new ArrayList();
        for (String str2 : c) {
            String queryParameter = parse.getQueryParameter(str2);
            if (!TextUtils.isEmpty(queryParameter)) {
                arrayList.add(new Pair(str2, queryParameter));
            }
        }
        Builder buildUpon = parse.buildUpon();
        buildUpon.clearQuery();
        Iterator it = arrayList.iterator();
        while (it.hasNext()) {
            Pair pair = (Pair) it.next();
            buildUpon.appendQueryParameter((String) pair.first, (String) pair.second);
        }
        buildUpon.appendQueryParameter("tt_info", new String(Base64.encode(AppLog.toEncryptByte(query), 8)));
        return buildUpon.build().toString();
    }

    public static String c(String str, String[] strArr) {
        if (TextUtils.isEmpty(str)) {
            return str;
        }
        Uri parse = Uri.parse(str);
        HashMap hashMap = new HashMap(strArr.length);
        for (String str2 : strArr) {
            String queryParameter = parse.getQueryParameter(str2);
            if (!TextUtils.isEmpty(queryParameter)) {
                hashMap.put(str2, queryParameter);
            }
        }
        Builder buildUpon = parse.buildUpon();
        buildUpon.clearQuery();
        for (String str3 : hashMap.keySet()) {
            buildUpon.appendQueryParameter(str3, (String) hashMap.get(str3));
        }
        return buildUpon.build().toString();
    }

    public static void d(StringBuilder stringBuilder, String str, String str2) {
        if (!TextUtils.isEmpty(str) && !TextUtils.isEmpty(str2)) {
            if (stringBuilder.toString().indexOf(63) < 0) {
                stringBuilder.append("?");
            } else {
                stringBuilder.append("&");
            }
            stringBuilder.append(str);
            stringBuilder.append(ContainerUtils.KEY_VALUE_DELIMITER);
            stringBuilder.append(Uri.encode(str2));
        }
    }
}
